package com.ems.dao;

import com.ems.model.Attendance;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AttendanceDAO {
    public boolean addAttendance(Attendance attendance) throws SQLException {
        String sql = "INSERT INTO attendances (emp_id, record_date, status, leave_reason) VALUES (?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, attendance.getEmpId());
            stmt.setDate(2, new java.sql.Date(attendance.getRecordDate().getTime()));
            stmt.setString(3, attendance.getStatus());
            stmt.setString(4, attendance.getLeaveReason());
            return stmt.executeUpdate() > 0;
        }
    }

    public boolean updateAttendance(Attendance attendance) throws SQLException {
        String sql = "UPDATE attendances SET emp_id=?, record_date=?, status=?, leave_reason=? WHERE attendance_id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, attendance.getEmpId());
            stmt.setDate(2, new java.sql.Date(attendance.getRecordDate().getTime()));
            stmt.setString(3, attendance.getStatus());
            stmt.setString(4, attendance.getLeaveReason());
            stmt.setInt(5, attendance.getAttendanceId());
            return stmt.executeUpdate() > 0;
        }
    }

    public boolean deleteAttendance(int attendanceId) throws SQLException {
        String sql = "DELETE FROM attendances WHERE attendance_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, attendanceId);
            return stmt.executeUpdate() > 0;
        }
    }

    public Attendance getAttendanceById(int attendanceId) throws SQLException {
        String sql = "SELECT * FROM attendances WHERE attendance_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, attendanceId);
            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    return new Attendance(
                            rs.getInt("attendance_id"),
                            rs.getString("emp_id"),
                            rs.getDate("record_date"),
                            rs.getString("status"),
                            rs.getString("leave_reason")
                    );
                }
            }
        }
        return null;
    }

    public List<Attendance> getAttendancesByEmployee(String empId) throws SQLException {
        List<Attendance> attendances = new ArrayList<>();
        String sql = "SELECT * FROM attendances WHERE emp_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, empId);
            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    attendances.add(new Attendance(
                            rs.getInt("attendance_id"),
                            rs.getString("emp_id"),
                            rs.getDate("record_date"),
                            rs.getString("status"),
                            rs.getString("leave_reason")
                    ));
                }
            }
        }
        return attendances;
    }

    public List<Attendance> getAllAttendances() throws SQLException {
        List<Attendance> attendances = new ArrayList<>();
        String sql = "SELECT * FROM attendances";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql);
             ResultSet rs = stmt.executeQuery()) {
            while (rs.next()) {
                attendances.add(new Attendance(
                        rs.getInt("attendance_id"),
                        rs.getString("emp_id"),
                        rs.getDate("record_date"),
                        rs.getString("status"),
                        rs.getString("leave_reason")
                ));
            }
        }
        return attendances;
    }
}